//
//  UserInfoController.m
//  KMedicine
//
//  Created by 曹牧 on 16/7/7.
//  Copyright © 2016年 caomu. All rights reserved.
//

#import "UserInfoController.h"

#define VIEWWIDTH self.view.frame.size.width


@interface UserInfoController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSArray *  contentArray;
@property (nonatomic,strong) NSArray * titleArray;

@property (nonatomic,weak) UIButton * protraitBtn;//头像button

@end

@implementation UserInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置title
    self.title = @"个人信息";
    
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.view.backgroundColor = [UIColor whiteColor];
    
}
- (void)setProtraitImage:(UIImage *)protraitImage{
    
    _protraitImage = protraitImage;
    
}
#pragma mark -
#pragma mark - section headerView
- (UIView *)getCustomView{
    
    //tableHeaderView
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, 90)];
    sectionView.backgroundColor = kThemeColor;

    //头像
    UIButton * protraitBtn = [[UIButton alloc ] initWithFrame:CGRectMake(0, 0, 80,80)];
    self.protraitBtn = protraitBtn;
    protraitBtn.center = CGPointMake(VIEWWIDTH*0.5, 60);
    
    //取出缓存图片
    UIImage *iconIMG = [UIImage imageWithContentsOfFile:kFilePath(@"userProtraitPicture.png")];
    
    [protraitBtn setImage:iconIMG forState:UIControlStateNormal];

    protraitBtn.layer.cornerRadius = 40;
    protraitBtn.layer.masksToBounds = YES;
    protraitBtn.layer.borderWidth = 2;
    protraitBtn.layer.borderColor = [UIColor grayColor].CGColor;
    
    [protraitBtn addTarget:self action:@selector(alertPortrait) forControlEvents:UIControlEventTouchUpInside];
    
    [sectionView addSubview:protraitBtn];
    return sectionView;

}



#pragma mark -
#pragma mark - 弹出提示框

- (void)alertPortrait{
    
    //弹出提示框
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    //从相册中选择
    UIAlertAction * albumAction = [UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
        
        pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        //允许编辑，即放大裁剪
        pickerController.allowsEditing = YES;
        //自代理
        pickerController.delegate = self;
        
        [self presentViewController:pickerController animated:YES completion:nil];
        
    }];
    
    //取消
    UIAlertAction * cancelAction= [UIAlertAction
                                   actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertController addAction:albumAction];
    
    [alertController addAction:cancelAction];
    
    //显示
    [self presentViewController:alertController animated:YES completion:nil];
    
}


//delegate method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    //    NSLog(@"%@",[info class]);
    UIImage * initImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    
    UIImage * editImage = [UIImage getMinImage:initImage ifNeedDrawSize:CGSizeMake(100, 100)];
    
    //data 写入文件
    [UIImagePNGRepresentation(editImage) writeToFile:kFilePath(@"userProtraitPicture.png") atomically:YES];

    [self.protraitBtn setImage:editImage forState:UIControlStateNormal];
    
    //将头像存入用户
    if (_returnImage) {
        _returnImage(editImage);
    }
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.offset(0);
    }];
}

#pragma mark -
#pragma mark - dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
        
    }
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.detailTextLabel.text = self.contentArray[indexPath.row];

    return cell;
    
}

#pragma mark -
#pragma mark - delegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [self getCustomView];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 120;
}


#pragma mark -
#pragma mark - lazy
- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
    }
    return _tableView;
    
}
- (NSArray *)contentArray{
    
    if (!_contentArray) {
        _contentArray = @[@"王二才",@"男",@"14",@"435678987678978765X",@"155600635511",@"北京"];
    }
    return _contentArray;
}

- (NSArray *)titleArray{
    
    if (!_titleArray) {
        _titleArray = @[@"姓名",@"性别",@"年龄",@"身份证",@"电话号码",@"地区"];
    }
    return _titleArray;
}

@end
